package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    private static final Logger log = LoggerFactory.getLogger(SetmealServiceImpl.class);
    @Autowired
    SetmealMapper setmealMapper;


    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 添加新套餐和新套餐里面的菜品
     * @param setmealDTO
     */
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //赋值给实体类
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();
        //添加多个套餐里面的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
       setmealDishes.forEach(setmealDish -> {
           setmealDish.setSetmealId(id);
            setmealDishMapper.insertSetmealDish(setmealDish);
       });
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult getSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开始分页查询,第一个是页码，第二个是每页显示记录数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //调用mapper方法，必须使用Page类来接收返回值
        Page<SetmealVO> page = setmealMapper.select(setmealPageQueryDTO);
        //通过page获取
        long total = page.getTotal();
        List<SetmealVO> list = page.getResult();
        return new PageResult(total,list);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
        //查询套餐
        SetmealVO setmealVO = setmealMapper.selectById(id);
        //查询套餐里面的菜品并封装
        setmealVO.setSetmealDishes(setmealDishMapper.selectSetmealById(id));

        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void updataSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐
        setmealMapper.updataSetmeal(setmeal);
        //根据套餐id删除套餐里面的菜品
        Long id = setmealDTO.getId();
        log.info("套餐对应的id：{}",id);
        setmealDishMapper.delectSetmealDish(id);
        //新增套餐里面的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
            setmealDishMapper.insertSetmealDish(setmealDish);
        });
    }

    /**
     * 删除套餐及套餐里面的菜品
     * @param ids
     */
    @Override
    public void delectSetmeal(String[] ids) {
        for (String id : ids) {
            //删除套餐
            setmealMapper.delectSetmeal(id);
            //删除套餐里面的菜品
            setmealDishMapper.delectSetmealDish(Long.parseLong(id));
        }
    }

    @Override
    public List<Setmeal> getSetmealByCategoryId(String categoryId) {
        List<Setmeal> setmealList = setmealMapper.selectByCategoryId(categoryId);

        return setmealList;
    }
}
